package rs.webnet.android.kamere;

import java.util.ArrayList;
import java.util.List;

import rs.webnet.android.kamere.data.DataFetcher;
import rs.webnet.android.kamere.data.Point;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class CamerasMapActivity extends MapActivity {
	MapView mapView;
	private MyLocationOverlay selfLocation;
	private MapController mapController;
	private LocationListener locationListener;
	public LocationManager loc;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.camerasmap_layout);
        mapView=(MapView)findViewById(R.id.mapview);        
        mapView.setBuiltInZoomControls(true);
		mapController = mapView.getController();
		mapController.setZoom(15);
		
        List<Overlay> list = mapView.getOverlays();
        selfLocation = new MyLocationOverlay(this, mapView);
		selfLocation.enableMyLocation();
		selfLocation.enableCompass();
		selfLocation.runOnFirstFix(new Runnable() {
            public void run() {
                mapController.animateTo(selfLocation.getMyLocation());
            }
        });
		list.add(selfLocation);
	
		this.setObjectsPoi(list);
    }
    @Override
    public void onResume(){
    	super.onResume();
    	selfLocation.enableMyLocation();
    }
    @Override
    public void onPause(){
    	super.onPause();
    	selfLocation.disableMyLocation();
    }
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	private GeoPoint getMyLocation() {
		/* Gleda u prefs da li da koristi GPS ili ne i setuje locationProvider */
		String locationProvider;
		if (PreferencesManager.getUseGps(getApplicationContext()) != true) {
			locationProvider = LocationManager.NETWORK_PROVIDER;
		} else {
			locationProvider = LocationManager.GPS_PROVIDER;
		}

		loc = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
	//	loc.requestLocationUpdates(locationProvider, 0, 0, locationListener);
		Location location = loc.getLastKnownLocation(locationProvider);
		
		//TODO pravi problem kada ni jedna app nije koristila GPS pa je getLastKnown null
		
		Double lat = location.getLatitude() * 1E6;
		Double lng = location.getLongitude() * 1E6;
		GeoPoint center = new GeoPoint(lat.intValue(), lng.intValue());
		return center;
	}
	private void setObjectsPoi(List<Overlay> list) {
		MyItemizedOverlay overlay = new MyItemizedOverlay(getResources().getDrawable(R.drawable.pushpin));
		for(Point point:DataFetcher.cameras){
			Double lat = Double.parseDouble(point.getLat()) * 1E6;
			Double lng = Double.parseDouble(point.getLng()) * 1E6;
			
			OverlayItem item = new OverlayItem(new GeoPoint(lat.intValue(), lng.intValue()), "My Location", null);
			overlay.addItem(item);
	//		ObjectPoint objPoint=new ObjectPoint(new GeoPoint(lat.intValue(), lng.intValue()));			
	//		list.add(objPoint);
		}
		list.add(overlay);
	}
	class MyItemizedOverlay extends ItemizedOverlay {
		 
		private List<OverlayItem> items;
		private Drawable marker;
	 
		public MyItemizedOverlay(Drawable defaultMarker) {
			super(defaultMarker);
			items = new ArrayList();
			marker = defaultMarker;
		}
	 
		@Override
		protected OverlayItem createItem(int index) {
			return (OverlayItem)items.get(index);
		}
	 
		@Override
		public int size() {
			return items.size();	 
		}	 
		/*
		 * (non-Javadoc)
		 *
		 * @see
		 * com.google.android.maps.ItemizedOverlay#draw(android.graphics.Canvas,
		 * com.google.android.maps.MapView, boolean)
		 */
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
	 
		}
	 
		public void addItem(OverlayItem item) {
			items.add(item);
			populate();
		}
		@Override
		protected boolean onTap(int index) {
				OverlayItem item = items.get(index);
				Log.d("d",""+DataFetcher.cameras.get(index).getTitle());

		         Intent i = new Intent(CamerasMapActivity.this, PointActivity.class);
		         i.putExtra("title", DataFetcher.cameras.get(index).getTitle());
		         i.putExtra("description", DataFetcher.cameras.get(index).getDescription());
		         i.putExtra("image", DataFetcher.cameras.get(index).getImage());
		         startActivity(i);
				return true;
		    }
	 
	}
	/**
	 * Instantiate LocationListener
	 */
	private void getListener() {
		locationListener = new LocationListener() {
			@Override
			public void onLocationChanged(Location location) {
				Log.d("d", "Location changed:\n" + location.getAccuracy() + "\n" + location.getLatitude() + "\n" + location.getLongitude());
		//		loc.removeUpdates(locationListener);
			}

			@Override
			public void onProviderDisabled(String provider) {
			}

			@Override
			public void onProviderEnabled(String provider) {
			}

			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
			}
		};
	}
}
